package com.payment.system.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.payment.common.base.AjaxResult;
import com.payment.common.support.Convert;
import com.payment.common.utils.DateUtils;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.MerchantMonth;
import com.payment.system.mapper.MerchantMapper;
import com.payment.system.mapper.MerchantMonthMapper;
import com.payment.system.service.IMerchantMonthService;
import com.payment.system.vo.DayMonthVo;

/**
 * 月统计 服务层实现
 *
 * @author payment
 * @date 2019-03-07
 */
@Service
public class MerchantMonthServiceImpl implements IMerchantMonthService {
    @Autowired
    private MerchantMonthMapper merchantMonthMapper;
    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 查询月统计信息
     *
     * @param id 月统计ID
     * @return 月统计信息
     */
    @Override
    public MerchantMonth selectMerchantMonthById(Long id) {
        return merchantMonthMapper.selectMerchantMonthById(id);
    }

    /**
     * 查询月统计列表
     *
     * @param merchantMonth 月统计信息
     * @return 月统计集合
     */
    @Override
    public List<MerchantMonth> selectMerchantMonthList(MerchantMonth merchantMonth) {
        return merchantMonthMapper.selectMerchantMonthList(merchantMonth);
    }

    /**
     * 新增月统计
     *
     * @param merchantMonth 月统计信息
     * @return 结果
     */
    @Override
    public int insertMerchantMonth(MerchantMonth merchantMonth) {
        return merchantMonthMapper.insertMerchantMonth(merchantMonth);
    }

    /**
     * 修改月统计
     *
     * @param merchantMonth 月统计信息
     * @return 结果
     */
    @Override
    public int updateMerchantMonth(MerchantMonth merchantMonth) {
        return merchantMonthMapper.updateMerchantMonth(merchantMonth);
    }

    /**
     * 删除月统计对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMerchantMonthByIds(String ids) {
        return merchantMonthMapper.deleteMerchantMonthByIds(Convert.toStrArray(ids));
    }


    /**
     * 我的交易  月
     * @param pageNum
     * @param pageSize
     * @param merchantId
     * @return
     */
    @Override
    public AjaxResult selectMerchantMonthListByMerchantId(Integer pageNum, Integer pageSize, String merchantId) {
        if (pageNum == null || pageSize == null || StringUtils.isEmpty(merchantId)) {
            return AjaxResult.error(1, "缺失必要参数");
        }
        List<DayMonthVo> list = merchantMonthMapper.selectMerchantMonthListByMerchantId(pageNum * pageSize, pageSize, merchantId);

        return AjaxResult.successAndData("成功", list);
    }

    /**
     * 报表统计
     * @param merchantId
     * @return
     */
    @Override
    public AjaxResult selectReportFormTotal(String merchantId,String phone) {
        if (StringUtils.isEmpty(merchantId)) {
            return AjaxResult.error(1, "缺失必要参数");
        }
        List<Map<String, Object>> list = merchantMonthMapper.selectReportFormTotal(merchantId);
        for (Map<String, Object> map : list) {
        	 Integer dayAdditions = merchantMapper.selectAllTeamByParentphoneAndCreateDate(phone, map.get("time").toString());
        	 map.put("number", dayAdditions.toString());
        }
        if (list.size() < 6) {
            int c = 6 - list.size()+1;
            for (int i = 1; i < c; i++) {
            	Calendar month = Calendar.getInstance();
        		month.setTime(new Date());
        		month.add(Calendar.MONTH, -i);
        		Map<String, Object> map = new HashMap<>();
                map.put("number", 0);
                map.put("money", 0);
                map.put("profit", 0);
                map.put("time", DateUtils.parseDateToStr(DateUtils.YYYY_MM, month.getTime()));
                list.add(map);
            }

        }
        return AjaxResult.successAndData("成功", list);
    }
}
